package edu.ynu.se.xiecheng.achitectureclass.GreenHouse.GreenhouseController;


import edu.ynu.se.xiecheng.achitectureclass.GreenHouse.GreenhouseDao.BigUserDAO;
import edu.ynu.se.xiecheng.achitectureclass.GreenHouse.GreenhouseEntity.BigUser;
import edu.ynu.se.xiecheng.achitectureclass.GreenHouse.GreenhouseService.BigUserService;
import edu.ynu.se.xiecheng.achitectureclass.common.controller.LogicController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class BigUserController extends LogicController<BigUserService, BigUserDAO, BigUser, Long> {

    public BigUserController(BigUserService service) {
        super(service);
    }

    @GetMapping("/getByUsername")
    @ApiOperation(value = "通过用户名获取用户信息", notes = "根据用户名获取用户信息")
    public BigUser getByUsername(@RequestParam String username) {
        return service.findByUsername(username);
    }

//    @PostMapping("/create")
//    @ApiOperation(value = "创建用户", notes = "创建一个新用户")
//    public BigUser createUser(@RequestBody BigUser user) {
//        return service.POST(user);
//    }

    @PostMapping("/create")
    public ResponseEntity<?> createUser(@RequestBody BigUser user) {
        try {
            service.POST(user);
            return ResponseEntity.ok("注册成功");
        } catch (DataIntegrityViolationException e) {
            // 捕获唯一约束冲突异常
            return ResponseEntity.status(HttpStatus.CONFLICT).body("用户名或邮箱已存在");
        }
    }
}